using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Scalextric
{
    public class CarIDTrackUpdate
    {
        public string CarID { get; set; }
        public bool GameTime { get; set; }
        public bool Invalid { get; set; }

        public CarIDTrackUpdate(byte data)
        {
            BitArray bData = new BitArray(data);
            if (bData.Count != 8)
                throw new Exception();
            if (bData[0] && bData[1] && bData[2])
                Invalid = true;
            if (!bData[0] && !bData[1] && !bData[2])
                GameTime = true;
            //is car id collection number or three digit id?
            //if not
            CarID = bData[0].ToString() + bData[1].ToString() + bData[2].ToString();
            //if is sum
            bool[] cData = new bool[3];
            for (int i = 0; i < 2; i++)
            {
                cData[i] = bData[i];
            }
            CarID = getIntFromBitArray(new BitArray(cData)).ToString();
        }

        private int getIntFromBitArray(BitArray bitArray)
        {
            int[] array = new int[1];
            bitArray.CopyTo(array, 0);
            return array[0];
        }
    }
}
